//
//  FundListController.m
//  LeaveLight
//
//  Created by 张 on 2019/3/5.
//  Copyright © 2019年 xsl. All rights reserved.
//

#import "FundListController.h"
#import "FundListCell.h"

#import "ListTapAlertView.h"

@interface FundListController ()<UITableViewDelegate,UITableViewDataSource>
@property (assign,nonatomic) BOOL setupUI;
@property (assign,nonatomic) BOOL dataIsLoad;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic) NSIndexPath *selectedIndexpath;
@property (strong,nonatomic) ListTapAlertView *alertView;
@property (strong,nonatomic) NSMutableArray *tableData;
@property (assign,nonatomic) NSInteger page;

@end

@implementation FundListController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = UIColorMakeWithHex(@"#F5F5F5");
    [self configTableView];
}

- (ListTapAlertView *)alertView{
    if (!_alertView) {
        _alertView = [[ListTapAlertView alloc] initWithFrame:CGRectMake(100, (SCREEN_WIDTH - 40)*200.0/335.0, 80, 35)];
        weakify(self);
        _alertView.tapBtnPressedBlock = ^(NSInteger paressedIndex) {
            strongify(self);
            ZDBaseModel *model = self.tableData[self.selectedIndexpath.row];
            if (paressedIndex == 0) {
                UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
                pasteboard.string = model.content;
            }
            else{
                [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_WechatTimeLine),@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_QQ)]];
                [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
                    // 根据获取的platformType确定所选平台进行下一步操作
                    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
                    UMShareImageObject * shareObject = [UMShareImageObject shareObjectWithTitle:@"留光" descr:@"一抹时光,一抹情" thumImage:[[SDWebImageManager sharedManager].imageCache imageFromCacheForKey:model.images]];
                    shareObject.shareImage = [[SDWebImageManager sharedManager].imageCache imageFromCacheForKey:model.images];
                    shareObject.title = @"留光";
                    shareObject.descr = @"一抹时光,一抹情";
                    //分享消息对象设置分享内容对象
                    messageObject.shareObject = shareObject;
                    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
                        if (error) {
                            [QMUITips showError:@"分享失败"];
                        }else{
                            [QMUITips showSucceed:@"分享成功"];
                        }
                    }];
                }];
            }
        };
    }
    return _alertView;
}

- (void)configTableView
{
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.tableView registerNib:[UINib nibWithNibName:@"FundListCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([FundListCell class])];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.estimatedRowHeight = 140;
    [self.tableView reloadData];
    
    weakify(self);
    self.tableView.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        strongify(self);
        self.page = 1;
        [self loadChildData];
        
    }];
    
    MJRefreshAutoNormalFooter * footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        strongify(self);
        self.page++;
        [self loadChildData];
    }];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    [footer setTitle:@"" forState:MJRefreshStatePulling];
    self.tableView.mj_footer = footer;
}

- (void)creatUIAndLoadData{
    if (!_dataIsLoad) {
        self.page = 1;
        [self.tableView.mj_header beginRefreshing];
    }
    else{
        NSLog(@"数据已获取");
    }
}

- (void)loadChildData{
    [self getLeavelightListWithcategoryid:[NSString stringWithFormat:@"%ld",self.classId] Success:^(NSMutableArray * _Nonnull listArr) {
        self.dataIsLoad = YES;
        if (self.page <= 1) {
            self.tableData = listArr;
        }
        else{
            [self.tableData addObjectsFromArray:listArr];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failed:^(NSString * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - <UITableViewDelegate,UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.tableData ? self.tableData.count : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FundListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FundListCell class])];
//    UILongPressGestureRecognizer * longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(cellLongPress:)];
//    longPressGesture.minimumPressDuration = 1.0f;//设置长按 时间
//    [cell addGestureRecognizer:longPressGesture];
    if (self.tableData) {
        TemplateModel *templateModel = [[TemplateModel alloc] init];
        templateModel.chicken_model = self.tableData[indexPath.row];
        [cell updateUIWithModel:templateModel];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.alertView removeFromSuperview];
    TemplateModel *templateModel = [[TemplateModel alloc] init];
    templateModel.chicken_model = self.tableData[indexPath.row];
    EditController * edit = [[EditController alloc]initWithNibName:@"EditController" bundle:nil];
    edit.model = templateModel;
    [self.navigationController pushViewController:edit animated:YES];
}

-(void)cellLongPress:(UILongPressGestureRecognizer *)longRecognizer{
    if (longRecognizer.state==UIGestureRecognizerStateBegan) {
        //成为第一响应者，需重写该方法
        [self becomeFirstResponder];
        CGPoint location = [longRecognizer locationInView:self.tableView];
        NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:location];
        //可以得到此时你点击的哪一行
        self.selectedIndexpath = indexPath;
        FundListCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        [cell addSubview:self.alertView];
        //在此添加你想要完成的功能
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.alertView removeFromSuperview];
}

#pragma mark  实现成为第一响应者方法
-(BOOL)canBecomeFirstResponder{
    return YES;
}



@end
